package com.fyzk.activity.lottery.jc.football;


import android.view.Gravity;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.fyzk.activity.lottery.jc.football.adapter.JCZQ_BF_ZhiFuAdapter;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanInfor;
import com.fyzk.activity.lottery.jc.football.newJz.BF_Sel_Pop;
import com.fyzk.activity.lottery.zc.tz_dialog.ZCBF_infor;
import com.fyzk.activity.zhifu.SportsFootballPayActivity;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.ScreenUtils;
import com.fyzk.utils.Utils;

import java.util.List;
import java.util.Set;

import static com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog.result;

public class JCZQ_BF_ZhiFuActivity extends SportsFootballPayActivity {

    @Override
    protected void onResume() {
        super.onResume();
        buyType.setText("竞彩足球-比分");
    }

    @Override
    protected void clear() {
        super.clear();
        for (int i = 0; i < infos.size(); i++) {
            JCInfo info = infos.get(i);
            if (info.getOnclikNum() > 0) {
                info.setOnclikNum(0);
                Set<String> keys = info.btnHGPojo.keySet();
                for (String key : keys) {
                    info.btnHGPojo.get(key).isSel = false;
                }
            }
            List<ZCBF_infor> touZhu = info.getTouZhuInfor();
            for (int j = 0; j < touZhu.size(); j++) {
                touZhu.get(j).setSelect(false);
            }
            info.setResult("");
        }
        adapter.setInfors(infos, isDanguan);
    }

    @Override
    protected void initAdapter() {
        adapter = new JCZQ_BF_ZhiFuAdapter(this, isDanguan, infos, handler, this);
        listView.setAdapter(adapter);

    }

    /**
     * 获取投注内容	301012001-20140304002(13)|20140304007(32,11)^
     *
     * @param infos
     */
    public String getContent(List<JCInfo> infos, boolean isdanguan) {
        List<String> chuans = JC_GuoGuanInfor.getChuanIds(result, isdanguan);
        String content = "";
        for (int i = 0; i < chuans.size(); i++) {
            content += lotteryId + "" + wanFa + chuans.get(i) + "-";//301012001-

            String n = "";
            String danCodes = "";
            for (int j = 0; j < infos.size(); j++) {
                JCInfo info = infos.get(j);
                String danChang = "";


                if (!info.getResult().equals(RESULT_MOREN)) {
                    danChang = info.getMatchNum() + "(";

                    List<String> results = JC_GuoGuanInfor.getBiFenIds(info.getResult());
                    for (String string : results) {
                        danChang += string.trim() + ",";
                    }

                    danChang = danChang.substring(0, danChang.length() - 1);
                    danChang += ")";
                }

                if (!danChang.equals("")) {
                    if (info.isDan()) {
                        danCodes += danChang + "|";
                    } else {
                        n += danChang + "|";
                    }
                }
            }
            if (!danCodes.equals("")) {
                danCodes = danCodes.substring(0, danCodes.length() - 1);
                danCodes += "#";
            }
            if (!n.equals("")) {
                n = n.substring(0, n.length() - 1);
                n += "^";
            }

            content += danCodes + n + "!";
        }

        if (!content.equals("")) {
            content = content.substring(0, content.length() - 1);
        }


        return content;
    }

    @Override
    protected int getMaxGuoGuan() {
        // TODO Auto-generated method stub
        return 4;
    }

    @Override
    public void open(final JCInfo info) {
        int height = Utils.dip2px(520f);
        BF_Sel_Pop bf_sel_pop = new BF_Sel_Pop(getContext(), WindowManager.LayoutParams
                .MATCH_PARENT, height, info);
        LogUtil.v("y=" + (ScreenUtils.getInstance().getHeight() - bf_sel_pop.getHeight()));
        LogUtil.v("p_height=" + bf_sel_pop.getHeight());
        setBackgroundAlpha(0.5f);
        bf_sel_pop.showAtLocation(getWindow().getCurrentFocus(), Gravity.NO_GRAVITY, 0, ScreenUtils.getInstance()
                .getHeight() -
                height);
        bf_sel_pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
                adapter.notifyDataSetChanged();
                if (info.getOnclikNum() == 0) {
                    onDelete(info);
                    adapter.refreshDan();
                }

            }
        });

    }


    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
}
